package com.example.tuter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tuter.R;
import com.example.tuter.activity.util.PageResultHandler;
import com.example.tuter.activity.util.PageResultHelper;
import com.example.tuter.adapter.CommentAdapter;
import com.example.tuter.entity.Comment;
import com.example.tuter.entity.Floor;
import com.example.tuter.entity.Message;
import com.example.tuter.entity.PageResult;
import com.example.tuter.util.Constant;
import com.example.tuter.util.UpdateUIUtil;
import com.example.tuter.util.httpUtil.HttpFunction.CommunityAPI;

import retrofit2.Call;
import retrofit2.Callback;

//查看一个具体楼层下的所有回复
public class ViewFloorActivity extends BaseActivity implements PageResultHandler<Comment> {


    private static final String TAG =ViewFloorActivity.class.getSimpleName();
    private Floor floor;



    private EditText editText;

    private final PageResultHelper<Comment> pageHelper = new PageResultHelper<Comment>();
    private CommunityAPI communityAPI;
    private CommentAdapter commentAdapter;
    private LinearLayoutManager linearLayoutManager;
    private ImageView button_send;
    private ImageView imageView_comment_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }




    //发送回复
    public void sendComment(String content) {
        Call<Message<Comment>> call = communityAPI.sendComment(floor.getFid(), content);
        call.enqueue(new Callback<Message<Comment>>() {
            @Override
            public void onResponse(retrofit2.Call<Message<Comment>> call, retrofit2.Response<Message<Comment>> response) {
                editText.setText("");
                Toast.makeText(ViewFloorActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
                ViewFloorActivity.this.onCreate(null);
            }

            @Override
            public void onFailure(retrofit2.Call<Message<Comment>> call, Throwable t) {

            }
        });
    }

    @Override
    public Call<Message<PageResult<Comment>>> getDataByPage(Integer pageNo) {
        return communityAPI.getComment(floor.getFid(), pageNo);
    }

    @Override
    public void updateUI() {
        UpdateUIUtil.updateUIBySingleAdapter(commentAdapter);
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        floor = (Floor) intent.getSerializableExtra("floor");


        communityAPI = Constant.r.create(CommunityAPI.class);
    }

    @Override
    protected void bindEvent() {
        //发送帖子
        button_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!editText.getText().toString().isEmpty()) {//输入不为空
                    //send
                    sendComment(editText.getText().toString());//发送评论
                } else {
                    Toast.makeText(ViewFloorActivity.this, "不能发送空内容", Toast.LENGTH_SHORT).show();
                }
            }
        });

        imageView_comment_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_view_comment);


        editText = findViewById(R.id.comment_send_edit);

        button_send = findViewById(R.id.comment_send_button);

        imageView_comment_back = findViewById(R.id.recomment_toolbar_imageView);


        TextView title = findViewById(R.id.recomment_toolbar_textView);


        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.comment_recycleView);

        linearLayoutManager = new LinearLayoutManager(this);

        commentAdapter = new CommentAdapter(pageHelper.getData(), this);

        recyclerView.setLayoutManager(linearLayoutManager);

        recyclerView.setAdapter(commentAdapter);

        pageHelper.addPageScroll(this, recyclerView);

        title.setText(floor.getComments()+"");
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_view_comment;
    }
}
